package com.pybox.service;

import com.pybox.common.utils.JdbcTemplateUtils;
import com.pybox.dto.TEventCategroyDto;
import com.pybox.entity.TEventCategroy;
import com.pybox.repository.TEventCategroyRepository;
import com.pybox.spec.TEventCategroySpec;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：TEventCategroy Service
 *
 * @author wuyingkai
 * @date 2018-09-15
 */

@Service
@Transactional
public class TEventCategroyService {

    @Autowired
    private TEventCategroyRepository repository;

    @Autowired
    private JdbcTemplateUtils jdbcTemplateUtils;

    /**
     * 创建TEventCategroy
     *
     * @param source
     * @return
     */
    public boolean create(TEventCategroyDto source) {

        TEventCategroy target = new TEventCategroy();
        BeanUtils.copyProperties(source, target);
        repository.save(target);

        return true;

    }

    /**
     * 根据ID删除TEventCategroy
     *
     * @param id
     * @return
     */
    public boolean delete(Integer id) {

        repository.delete(id);

        return true;

    }

    /**
     * 更新TEventCategroy
     *
     * @param id
     * @param source
     * @return
     */
    public boolean update(Integer id, TEventCategroyDto source) {

        TEventCategroy exist = repository.findOne(id);
        if (null == exist) {
            throw new RuntimeException("记录不存在");
        }

        BeanUtils.copyProperties(source, exist, new String[]{"id"});
        repository.save(exist);

        return true;

    }

    /**
     * 根据ID查询TEventCategroy
     *
     * @param id
     * @return
     */
    public TEventCategroyDto detail(Integer id) {

        TEventCategroy exist = repository.findOne(id);
        if (null == exist) {
            throw new RuntimeException("记录不存在");
        }

        TEventCategroyDto target = new TEventCategroyDto();
        BeanUtils.copyProperties(exist, target);

        return target;

    }

    /**
     * 分页查询查询TEventCategroy
     *
     * @param pageNo
     * @param pageSize
     * @param paramMap
     * @return
     */
    public Page<TEventCategroyDto> page(Integer pageNo, Integer pageSize, Map<String, String> paramMap) {

        Pageable pageable = new PageRequest(pageNo - 1, pageSize);
        List<Object> object = new ArrayList<>();
        Page<TEventCategroyDto> data = jdbcTemplateUtils.page("select id,name from t_event_categroy where name is not null ",object,TEventCategroyDto.class,pageNo,pageSize);


//        Page<TEventCategroy> data = repository.findAll(new TEventCategroySpec(paramMap), pageable);
//
//        List<TEventCategroyDto> list = new ArrayList<>();
//
//        for (TEventCategroy obj : data.getContent()) {
//            TEventCategroyDto dto = new TEventCategroyDto();
//            BeanUtils.copyProperties(obj, dto);
//            list.add(dto);
//        }

        Page<TEventCategroyDto> page = new PageImpl<>(data.getContent(), pageable, data.getTotalElements());

        return page;

    }
}